package com.api;

import com.alibaba.fastjson.JSONObject;
import com.component.domain.MChannel;
import com.component.enumitem.NoteAddTypeEnum;
import com.component.service.MChannelService;
import com.component.service.NoteService;
import com.system.common.Result;
import com.system.security.SecurityUtil;
import com.system.security.domain.SelfUserEntity;
import com.system.util.EnumToListConverter;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Classname ChannelApi
 * @Description TODO
 * @Date 2023/9/17 23:23
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController
@RequestMapping("channel")
public class ChannelApi {

    @Autowired
    private NoteService noteService;

    @Autowired
    private MChannelService mChannelService;

    @GetMapping("gettexttype")
    public JSONObject getTextType(){
        NoteAddTypeEnum[] noteAddTypeEnums=NoteAddTypeEnum.values();
        List<Map<String,Object>> mapList=EnumToListConverter.convert(noteAddTypeEnums,"type","typeName");
        return Result.successInfo(mapList);
    }

    @GetMapping("getid")
    public JSONObject getById(@RequestParam("id")String id){
        MChannel mChannel=mChannelService.getById(id);
        return Result.successInfo(mChannel);
    }

    @PostMapping("/delete")
    public JSONObject deleteChannel(@RequestBody JSONObject json){
        Object id=json.get("id");
        if(id==null){
            return Result.errorInfo("栏目id不得为空");
        }
        int count=mChannelService.getMchannelChildsCountById(String.valueOf(id));
        if(count > 0){
            return Result.errorInfo("栏目下存在子栏目，不允许删除");
        }
        count=noteService.getNoteCountByChannelId(String.valueOf(id));
        if(count > 0){
            return Result.errorInfo("栏目下存在文章数据，不允许删除");
        }
        mChannelService.removeById(String.valueOf(id));
        return Result.successInfo("删除成功");
    }


    @GetMapping("/getchannel")
    public JSONObject getChannel(){
        List<MChannel> mChannels=mChannelService.getTreeArray();
        return Result.successInfo("success",mChannels);
    }


    @GetMapping("/getprichannel")
    public JSONObject getPriChannel(){
        List<MChannel> mChannels = mChannelService.getPriTreeArray(true);
        return Result.successInfo("success",mChannels);
    }

    @GetMapping("getprichannelbind")
    public JSONObject getPriChannelBind(){
        List<MChannel> mChannels = mChannelService.getPriTreeArray(false);
        return Result.successInfo("success",mChannels);
    }

    @GetMapping("/getallchannel")
    public JSONObject getAllChannel() {
        List<MChannel> mChannels = mChannelService.getAllTreeArray();
        return Result.successInfo("success",mChannels);
    }



    @PostMapping("/update")
    public JSONObject updateController(@RequestBody JSONObject obj){
        if(obj.get("id")==null && "".equals(obj.get("id"))){
            return Result.errorInfo("栏目id不得为空");
        }
        if(obj.get("channelname")==null && "".equals(obj.get("channelname"))){
            return Result.errorInfo("栏目名称不得为空");
        }
        String id=obj.get("id").toString();
        MChannel mChannel=mChannelService.getById(id);
        if(mChannel==null){
            return Result.errorInfo("栏目不存在");
        }
        if(obj.get("channelpath")==null && "".equals(obj.get("channelpath"))){
            return Result.errorInfo("栏目路径不得为空");
        }
        String channelpath=obj.get("channelpath").toString();
        if(!mChannel.getChannelpath().equals(channelpath)){
            MChannel m=mChannelService.getMChannelByPath(channelpath);
            if(m != null){
                return Result.errorInfo(channelpath+" 路径已存在");
            }
        }
        //判定栏目id是否存在
        if(obj.get("channelpid")==null && "".equals(obj.get("channelpid"))){
            mChannel.setPid(0);
        }else {
            String channelpid=obj.get("channelpid").toString();
            MChannel pidMChannel1=mChannelService.getById(channelpid);
            if(pidMChannel1==null){
                mChannel.setPid(0);
            }
        }
        String channelName=obj.get("channelname").toString();
        mChannel.setChannelpath(channelpath);
        mChannel.setChannelname(channelName);
        String textType=(String) obj.get("textType");
        if(textType!=null){
            mChannel.setTextType(textType);
        }
        mChannelService.updateById(mChannel);
        return Result.successInfo("修改成功");
    }

    @PostMapping("add")
    public JSONObject addChannel(@RequestBody JSONObject obj){
        if(obj.get("channelname")==null && "".equals(obj.get("channelname"))){
            return Result.errorInfo("栏目名称不得为空");
        }
        if(obj.get("channelpath")==null && "".equals(obj.get("channelpath"))){
            return Result.errorInfo("栏目路径不得为空");
        }
        if(obj.get("ispri")==null){
            return Result.errorInfo("是否私有的不得为空");
        }
        String textType=(String) obj.get("textType");
        Boolean ispri=obj.getBoolean("ispri");
        String channelpath=obj.get("channelpath").toString();
        MChannel m=mChannelService.getMChannelByPath(channelpath);
        if(m != null){
            return Result.errorInfo(channelpath+" 路径已存在");
        }
        MChannel mChannel=new MChannel();
        if(obj.get("channelpid")!=null && !"".equals(obj.get("channelpid"))){
            String pid=obj.get("channelpid").toString();
            MChannel p=mChannelService.getById(pid);
            if(p==null){
                return Result.errorInfo(pid+"选中的父栏目不存在");
            }
            mChannel.setPid(Integer.parseInt(pid));
        }else {
            mChannel.setPid(0);
        }
        SelfUserEntity userEntity=SecurityUtil.getSelfUserEntity();
        mChannel.setUserid(userEntity.getUserId().toString());
        String channelname=obj.get("channelname").toString();
        mChannel.setChannelname(channelname);
        mChannel.setChannelpath(channelpath);
        mChannel.setIsprivate(ispri);
        if(textType==null){
            mChannel.setTextType(NoteAddTypeEnum.MD_EDITOR_TYPE_ENUM.getType());
        }else {
            mChannel.setTextType(textType);
        }
        mChannelService.save(mChannel);
        return Result.successInfo("新增成功");
    }
}
